home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / info-service / gopher / Unix / GopherTools / logger.pl < prev    next >
Encoding:
Perl Script  |  1993-07-20  |  5.5 KB  |  212 lines

  1. #!/usr/local/bin/perl
  2. #--------------------------------- Logger.pl ----------------------------------
  3. #
  4. #  Perl script which supports:
  5. #    Logfile rotation up to a maximum number of days to be kept....
  6. #    A "guestbook" which tracks sites which hit on you, and how many times
  7. #      they've hit things.
  8. #    A primitive usage database which shows _what's_ getting hit
  9. #    A mailed report of "newbies" sent to the system administrator (ie.
  10. #      new sites which have hit you since the last report).
  11. #
  12. #  Making it work:
  13. #    Does NOT work with static daemon as it and the static daemon are
  14. #      presently set up.
  15. #    Change the parms below to something you find aesthetically pleasing.
  16. #    Put it up as a cron job to be run once per day.
  17. #
  18. #  General disclaimer of competence:
  19. #    I'm new at this (and I bet it shows), so send suggestions for 
  20. #    improvements to:   tom@law.mail.cornell.edu
  21. #
  22. #   "A Perl script is correct if it's halfway readable and gets
  23. #    the job done before your boss fires you"
  24. #                             -- Larry Wall
  25. #  
  26. #----------------------------------------------------------------------------
  27.  
  28. #--------------------------- Site-configured stuff --------------------------
  29. #  These could probably all be done from the command line, but so what.
  30. #  Set this to the full path and name of your gopher log file as you've
  31. #  set it in the -l parameter for gopherd:
  32.  
  33. $logbase="/home/mudhoney/GopherLog70";
  34.  
  35. #  This script will generate files gopherlog.1 .. gopherlog.nn depending
  36. #  on how many days you decide to keep, as in:
  37.  
  38. $keepfor=7;
  39.  
  40. #  Setting $keepfor to "" permits all the logging/reporting to take place
  41. #  but zaps the logfile.
  42. #  Next, pick a path and name for your guestbook (the file which records
  43. #  sites which have hit on you). Leave it empty ("") if you don't want a
  44. #  guestbook.
  45.  
  46. $guestbook="/home/mudhoney/gopherlogs/gopher_guests";
  47.  
  48. #  Next, pick a path and name for the usage file (the one which tracks
  49. #  which of your local choices gets hit on, and how much).
  50.  
  51. $usage="/home/mudhoney/gopherlogs/usage_log";
  52.  
  53. #  Finally, pick a person to receive mailed reports on new sites which hit
  54. #  you (or any other reports you might decide to have this thing generate).
  55. #  You can put more than one person here, separate them with semicolons
  56. #  if you want more than one.
  57.  
  58. $mail_to="lindner";
  59.  
  60. #  OK, down to business.
  61. #  Do file rotation; this has the useful effect that we won't be tallying
  62. #  from something gopher is trying to write to.
  63.  
  64. $i = $keepfor;
  65. LOGFILE:
  66. while ($i>0)
  67.     {
  68.     $i--;
  69.     next LOGFILE unless -e $logbase.".".$i;
  70.     system("mv ".$logbase.".".$i." ".$logbase.".".($i+1));
  71.     }
  72. system("cp ".$logbase." ".$logbase.".1");
  73. unlink $logbase;
  74.  
  75. #  Do the guestbook and usage.
  76. if ($guestbook || $usage)
  77.     {
  78.     open(INFILE,"< $logbase.1");
  79.     if ($guestbook && -T $guestbook)
  80.         {
  81.         open(GUESTBOOK, "< $guestbook") || die "Can't get the guestbook file.";
  82.         #load up the array
  83.         while (<GUESTBOOK>)
  84.             {
  85.             chop($line=$_);
  86.             ($site, $hits)=split(':',$line);
  87.             if (!defined($hits) || ($hits eq ""))
  88.                 {
  89.                 $hits = 1;
  90.                 }
  91.             $guests{$site}=$hits;
  92.             }
  93.         close GUESTBOOK;
  94.         }
  95.     if ($usage && -T $usage)
  96.         {
  97.         open(USAGE, "< $usage") || die "Can't get the usage file.";
  98.         #load up the array
  99.         while (<USAGE>)
  100.             {
  101.             chop($line=$_);
  102.             #The usage line may have more than one colon, 
  103.             #as with mail files from the disctool script,
  104.             # in which case we want the last field.
  105.  
  106.             @flds=split(':',$line);
  107.  
  108.             #the last field is always going to be the number of
  109.             #of hits, everything else should get joined into
  110.             #the key.
  111.             $action=join(':',@flds[0 .. $#flds-1]);
  112.             $activities{$action}=$flds[$#flds];
  113.             }
  114.         close USAGE;
  115.         }
  116.     LINE:
  117.     while(<INFILE>)
  118.         {
  119.         chop($line=$_);
  120.         if(!grep(/Root Connection| \//,$line))
  121.             {
  122.             next LINE; #skip anything but hits on server
  123.             }
  124.         ($prefix, $what)=split(/\s+:\s+/,$line);
  125.         ($dow, $mon, $day, $tm, $yr, $seq, $guest)=split(' ', $prefix);
  126.  
  127.         #don't think my clever and succinct code of the original
  128.         #version works very well.  Hafta go with something less
  129.         #quote elegant unquote.  How elegant is it if it doesn't
  130.         #work?
  131.  
  132.         if ($usage)  #see if anybody's done this before
  133.             {
  134.             if (defined $activities{$what})
  135.                 {
  136.                 $activities{$what}++;
  137.                 }
  138.             else    
  139.                 {
  140.                 $activities{$what}=1;
  141.                 $newactivs{$what}=1;
  142.                 }
  143.             }
  144.         if ($guestbook)
  145.             {
  146.             if (defined $guests{$guest})
  147.                 {
  148.                 $guests{$guest}++;
  149.                 }
  150.             else
  151.                 {
  152.                 $guests{$guest}=1;
  153.                 $newguests{$guest}=1;
  154.                 }
  155.             
  156.             }
  157.         }
  158.     close INFILE;
  159.     }
  160.  
  161. #write out.
  162. if ($usage)
  163.     {
  164.     open(USAGE, "> $usage") || die "Can't open usage file for output";
  165.     foreach $key (sort(keys %activities))
  166.         {
  167.         print USAGE $key,":",$activities{$key},"\n";
  168.         }
  169.     close USAGE;
  170.     }
  171. if ($guestbook)
  172.     {
  173.     open(GUESTBOOK, "> $guestbook") || die "Can't open guestbook file for output";
  174.     foreach $key (sort(keys %guests))
  175.         {
  176.         print GUESTBOOK $key, ":", $guests{$key},"\n";
  177.         }
  178.     close GUESTBOOK;
  179.     }
  180.  
  181. #do the mail thing 
  182. if ($mail_to)
  183.     {
  184.     # make a temp file
  185.     open(MAILIT, ">zzz_logtmp") || die "Can't open mail file";
  186.     print MAILIT "Report from Gopher log manager.\n\n";
  187.     print MAILIT "The following new activities were logged:\n\n";
  188.     foreach $key (sort(keys %newactivs))
  189.         {
  190.         print MAILIT "\t",$key,"\n";
  191.         }
  192.  
  193.     print MAILIT "\n\n The following new guests were logged:\n\n";
  194.     foreach $key (sort(keys %newguests))
  195.         {
  196.         print MAILIT "\t",$key, "\n";
  197.         }
  198.     close MAILIT;
  199.     @recipients=split(';',$mail_to);
  200.     foreach $rcpt(@recipients)
  201.         {
  202.         system "mail -s DailyGopherAbstract ".$rcpt." < zzz_logtmp";
  203.         }
  204.     unlink "zzz_logtmp";
  205.     }
  206.  
  207. #cleanup if needed.
  208. unlink $logbase.".1" unless $keepfor;
  209.  
  210.